<?php

namespace App\Services\Statistics;

use ReflectionClass;
use App\Models\Statistic;
use App\Jobs\RefreshStatisticJob;
use Illuminate\Support\Facades\Bus;

class HardCountRefresh
{
	protected function jobs()
	{
		$jobs = [];
		
		$hardCount = new ReflectionClass(HardCount::class);
		$methods = $hardCount->getMethods();
		
		foreach ($methods as $method) {
			$jobs[] = new RefreshStatisticJob($method->name);
		}
		return $jobs;
	}
	
	public function __invoke()
	{
		if (Statistic::isDenied()) return;
		
		$queue = app()->environment('local') ? 'default' : 'statistic';
		
		Bus::batch($this->jobs())
			->allowFailures()
			->name('statistic')
			->onQueue($queue)
			->dispatch();
	}
}
